# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=neovim
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.9.1
pkgrel=3
pkgdesc='Fork of Vim aiming to improve user experience, plugins, and GUIs (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url='https://neovim.io/'
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libluv"
         "${MINGW_PACKAGE_PREFIX}-libtermkey"
         "${MINGW_PACKAGE_PREFIX}-libuv"
         "${MINGW_PACKAGE_PREFIX}-libvterm"
         "${MINGW_PACKAGE_PREFIX}-luajit"
         "${MINGW_PACKAGE_PREFIX}-msgpack-c"
         "${MINGW_PACKAGE_PREFIX}-tree-sitter"
         "${MINGW_PACKAGE_PREFIX}-unibilium")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-lua51-lpeg"
             "${MINGW_PACKAGE_PREFIX}-lua51-mpack"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/neovim/neovim/archive/refs/tags/v${pkgver}.tar.gz"
        0001-cmake-disable-bundling.patch)
sha256sums=('8db17c2a1f4776dcda00e59489ea0d98ba82f7d1a8ea03281d640e58d8a3a00e'
            '89a7ac9bad682e9d1b008900ae4b1d2098bfa942d31ade7ea7283295b39aefe5')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}/0001-cmake-disable-bundling.patch"

  # Werror is enabled in CI only
  sed "s/-Werror/-Wall/g" -i src/nvim/CMakeLists.txt
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CFLAGS+=" -Wno-format -Wno-format-extra-args" \
  CXXFLAGS+=" -Wno-format -Wno-format-extra-args" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DUSE_BUNDLED_NVIMQT=OFF \
      "${extra_config[@]}" \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  ./bin/nvim.exe --version
  ./bin/nvim.exe --headless -u NONE -i NONE -c ':quit'
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"
}
